//****************************************************************
//* Copyright (c) 2008 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   CostStructureItemCategoryForm.java  $$
//* $$Revision:   1.2  $$
//* $$Author:   TMALDON4  $$
//* $$Date:   Feb 26 2009 07:30:54  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.view.form;

import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc05Itemcatgry;

/**
 * 
 * @author fcolucc1
 * 
 */
public class CostStructureItemCategoryForm extends ValidatorForm {
    private String section;
    private String codeFind;
    private String nameFind;
    private String code;
    private String name;
    private Collection<Teesc05Itemcatgry> itemCategories;
    private Teesc05Itemcatgry itemCategorySelected;

    public void resetForm() {
        this.section = null;
        this.codeFind = null;
        this.nameFind = null;
        this.code = null;
        this.name = null;
        this.itemCategories = null;
        this.itemCategorySelected = null;
    }

    /**
     * Limpia los campos utilizados en el form para la creacion o actualizacion
     * de Item Categories
     * 
     */
    public void clearCreateUpdateFields() {
        this.code = null;
        this.name = null;
        this.itemCategorySelected = null;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code.toUpperCase();
    }

    public String getCodeFind() {
        return codeFind;
    }

    public void setCodeFind(String codeFind) {
        this.codeFind = codeFind.toUpperCase();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name.toUpperCase();
    }

    public String getNameFind() {
        return nameFind;
    }

    public void setNameFind(String nameFind) {
        this.nameFind = nameFind.toUpperCase();
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public Collection<Teesc05Itemcatgry> getItemCategories() {
        return itemCategories;
    }

    public void setItemCategories(Collection<Teesc05Itemcatgry> itemCategories) {
        this.itemCategories = itemCategories;
    }

    public Teesc05Itemcatgry getItemCategorySelected() {
        return itemCategorySelected;
    }

    public void setItemCategorySelected(Teesc05Itemcatgry itemCategorySelected) {
        this.itemCategorySelected = itemCategorySelected;
    }

    /**
     * Setea el Item Category seleccionado para luego ser modificado
     * 
     * @param key
     * @param value
     */
    public void setEditMapped(String key, String value) {
        List<Teesc05Itemcatgry> itemCategories = (List<Teesc05Itemcatgry>) this
                .getItemCategories();

        this.setItemCategorySelected(itemCategories
                .get(Integer.parseInt(key) - 1));
    }

    /**
     * Setea el Item Category seleccionado para luego ser eliminado
     * 
     * @param key
     * @param value
     */
    public void setDeleteMapped(String key, String value) {
        setEditMapped(key, value);
    }

    /**
     * Setea los campos del cuadro de creacion/modificacion de Item Categories
     * 
     * @param economicSource
     */
    public void setItemCategoryForEdit(Teesc05Itemcatgry itemCategory) {
        this.setCode(String.valueOf(itemCategory.getEesc05ItemcatgryNumberK()));
        this.setName(itemCategory.getEesc05ItemcatgryNameN());
    }

    /**
     * Setea los campos del Item Category para su posterior creacion
     * 
     * @return Item Category con los campos minimos seteados para la posterior
     * creacion
     */
    public Teesc05Itemcatgry getItemCategoryForCreate() {
        Teesc05Itemcatgry itemCategory = new Teesc05Itemcatgry();

        itemCategory.setEesc05ItemcatgryNameN(this.getName());
        return itemCategory;
    }

    /**
     * Setea los campos del Item Category para su posterior actualizacion
     * 
     * @param itemCategory
     * Item Category a actualizar
     * @return Item Category con los campos a actualizar modificados
     */
    public Teesc05Itemcatgry getItemCategoryForUpdate(
                                                      Teesc05Itemcatgry itemCategory) {
        itemCategory.setEesc05ItemcatgryNameN(this.getName());

        return itemCategory;
    }

    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {
        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);

        if ("find".equalsIgnoreCase(this.getSection())) {
            if (StringUtils.isNotBlank(this.getCodeFind())) {
                if (this.getCodeFind().matches("[0-9]+")) {
                    try {
                        if (Integer.parseInt(this.getCodeFind()) == 0) {
                            ae
                                    .add(
                                            ActionMessages.GLOBAL_MESSAGE,
                                            new ActionMessage(
                                                    "errors.integergtzero",
                                                    resources
                                                            .getMessage("costStructure.itemCategory.label.code")));
                        }
                    } catch (NumberFormatException e) {
                        ae
                                .add(
                                        ActionMessages.GLOBAL_MESSAGE,
                                        new ActionMessage(
                                                "errors.invalid",
                                                resources
                                                        .getMessage("costStructure.itemCategory.label.code")));
                    }
                } else {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.integergtzero",
                                            resources
                                                    .getMessage("costStructure.itemCategory.label.code")));
                }
            }
        } else if ("create".equalsIgnoreCase(this.getSection())) {
            if (StringUtils.isBlank(this.getName())) {
                ae
                        .add(
                                ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "errors.required",
                                        resources
                                                .getMessage("costStructure.itemCategory.label.name")));
            }
        } else if ("update".equalsIgnoreCase(this.getSection())) {
            if (this.getItemCategorySelected() == null) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "error.noSelected.m", resources
                                .getMessage("costStructure.itemCategory")));
            } else if (StringUtils.isBlank(this.getName())) {
                ae
                        .add(
                                ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "errors.required",
                                        resources
                                                .getMessage("costStructure.itemCategory.label.name")));
            }
        }

        return ae;
    }

    public void cleanFindFields() {
        this.codeFind = null;
        this.nameFind = null;
    }

}
